package com.openroom.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.pivot.beans.BXMLSerializer;
import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.Application.UnprocessedKeyHandler;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.Keyboard.KeyLocation;
import org.apache.pivot.wtk.ScrollBar;
import org.apache.pivot.wtk.Theme;

import com.openroom.db.DBMigrationRunner;
import com.openroom.ui.component.BorderImage;
import com.openroom.ui.component.BorderImageSkin;
import com.openroom.ui.component.FullViewImage;
import com.openroom.ui.component.FullViewImageContainer;
import com.openroom.ui.component.FullViewImageContainerSkin;
import com.openroom.ui.component.FullViewImageSkin;
import com.openroom.ui.component.ZoomablePreviewImage;
import com.openroom.ui.component.skin.CustomScrollBarSkin;
import com.openroom.ui.component.skin.ZoomablePreviewImageSkin;

public class MainFrame implements Application, UnprocessedKeyHandler {

    private static final Logger LOGGER = Logger.getLogger(MainFrame.class);

    private MainWindow window = null;

    private OPMKeyHandler opmKeyHandler = new OPMKeyHandler();

    /**
     * @param args
     */
    public static void main(String[] args) {
        checkDBExists();
        Theme.getTheme().set(FocusableImageView.class, BorderImageSkin.class);
        Theme.getTheme()
                .set(PreviewImageView.class, PreviewImageViewSkin.class);
        Theme.getTheme().set(BorderImage.class, BorderImageSkin.class);
        Theme.getTheme().set(ZoomablePreviewImage.class, ZoomablePreviewImageSkin.class);
        Theme.getTheme().set(FullViewImageContainer.class, FullViewImageContainerSkin.class);
        Theme.getTheme().set(FullViewImage.class, FullViewImageSkin.class);
//        Theme.getTheme().set(ScrollBar.class, CustomScrollBarSkin.class);
        DesktopApplicationContext.main(MainFrame.class, args);
    }

    public static void checkDBExists() {
        File file = new File("./jphoto-manager.h2.db");
        File propFile = new File("./jphoto-manager.properties");
        if (propFile.exists()) {
            try {
                Properties prop = new Properties();
                prop.load(new FileInputStream(propFile));
                MainWindow.TEMP_DIR = prop.getProperty("TNAIL_DIR");
            } catch (Exception e) {
                LOGGER.error("error", e);
            }
        } else {
            new File("./jphoto-manager/tnails").mkdirs();
            MainWindow.TEMP_DIR = "./jphoto-manager/tnails";
            Properties prop = new Properties();
            prop.put("TNAIL_DIR", MainWindow.TEMP_DIR);
            try {
                prop.store(new FileWriter(propFile), "");
            } catch (IOException e) {
                LOGGER.error("error", e);
            }
        }
        if (!file.exists()) {
            DBMigrationRunner.createSchema();
        } else {
            Properties prop = new Properties();
            try {
                prop.load(new FileInputStream(propFile));
                String migNum = (String) prop.get("MIGRATION_NUM");
                if (migNum == null) {
                    migNum = "2";
                }
                int parseInt = Integer.parseInt(migNum);
                int upgradeNum = DBMigrationRunner.upgradeSchema(parseInt);
                prop.put("MIGRATION_NUM", ""+upgradeNum);
                prop.store(new FileWriter(propFile), "upgrade done");
            } catch (Exception e) {
                LOGGER.error("error", e);
            }
        }
    }

    @Override
    public void resume() throws Exception {

    }

    @Override
    public boolean shutdown(boolean arg0) throws Exception {
        if (window != null) {
            window.close();
        }
        return false;
    }

    @Override
    public void startup(Display display, Map<String, String> properties)
            throws Exception {
        BXMLSerializer bxmlSerializer = new BXMLSerializer();
        window = (MainWindow) bxmlSerializer.readObject(MainFrame.class,
                "main_window.xml");
        window.open(display);
    }

    @Override
    public void suspend() throws Exception {
    }

    @Override
    public void keyTyped(char character) {

    }

    @Override
    public void keyPressed(int keyCode, KeyLocation keyLocation) {
        opmKeyHandler.keyPressed(null, keyCode, keyLocation);
    }

    @Override
    public void keyReleased(int keyCode, KeyLocation keyLocation) {

    }

}
